package com.kaiwukj.android.ufamily.mvp.ui.page.order.refund;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerOrderComponent;
import com.kaiwukj.android.ufamily.di.module.OrderModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.event.OrderEvent;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.HouseKeeperResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.OrderDetailResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.OrderResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.order.OrderContract;
import com.kaiwukj.android.ufamily.mvp.ui.page.order.OrderPresenter;
import com.kaiwukj.android.ufamily.mvp.ui.widget.RefreshLayout;
import com.kaiwukj.android.ufamily.mvp.ui.widget.SpaceItemDecoration;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.OrderRefundListUrl)
public class OrderRefundListActivity extends BaseMvpActivity<OrderPresenter> implements OrderContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.refresh_layout)
    RefreshLayout mRefreshLayout;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.empty_view)
    QMUIEmptyView emptyView;

    private OrderRefundAdapter mAdapter;

    private int page = 1;
    private int pageSize = 12;
    private final int status = 7;

    private boolean isLoadingMore = false;

    private int eventPos = -1;

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_list_normal;
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerOrderComponent.builder()
                .appComponent(appComponent)
                .orderModule(new OrderModule(this))
                .build().inject(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle.setText("退款/售后");
        rvList.setLayoutManager(new LinearLayoutManager(this));
        rvList.addItemDecoration(new SpaceItemDecoration(15));
        mAdapter = new OrderRefundAdapter();
        rvList.setAdapter(mAdapter);

        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull com.scwang.smartrefresh.layout.api.RefreshLayout refreshLayout) {
                isLoadingMore = true;
                loadData(++page);
            }

            @Override
            public void onRefresh(@NonNull com.scwang.smartrefresh.layout.api.RefreshLayout refreshLayout) {
                loadData(page = 1);
            }
        });

        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.btn_order_delete:
                    eventPos = position;
                    mPresenter.deleteOrder((int) mAdapter.getItemId(position));
                    break;
                case R.id.btn_order_detail:
                    ARouter.getInstance().build(ARouterUrlKt.OrderDetailUrl)
                            .withInt("orderId", (int) mAdapter.getItemId(position))
                            .navigation();
                    break;
            }
        });

        mAdapter.setOnEvent((eventPos, resultId, event) -> {
            this.eventPos = eventPos;
            if (event == OrderRefundAdapter.DELETE) {
                showMessageOKCancel(this, "删除该记录?", v -> mPresenter.deleteOrder((int) resultId));
            } else {
                showMessageOKCancel(this, "取消退款申请?", v -> mPresenter.cancelRefund((int) resultId));
            }
        });

        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            ARouter.getInstance().build(ARouterUrlKt.OrderDetailUrl)
                    .withInt("orderId", (int) mAdapter.getItemId(position))
                    .navigation();
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        emptyView.show(true);
        loadData(page);
    }

    private void loadData(int page) {
        mPresenter.fetchList(status, page, pageSize);
    }

    @Override
    public void onTaskErr(int taskId) {
        super.onTaskErr(taskId);
        emptyView.show("请求异常", "");
        mRefreshLayout.finishRefresh(false);
        mRefreshLayout.finishLoadMore(false);
        isLoadingMore = false;
    }

    @Override
    public void onTaskSuccess(int taskId) {
        super.onTaskSuccess(taskId);
        if (taskId == OrderPresenter.DELETE || taskId == OrderPresenter.CANCEL_REFUND) {
            mAdapter.removeAt(eventPos);
            eventPos = -1;
        }
    }

    @Override
    public void onFetchOrder(List<OrderResult> results) {
        emptyView.hide();
        if (isLoadingMore) {
            isLoadingMore = false;
            mAdapter.addData(results);
            if (results.size() < pageSize) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                mRefreshLayout.finishLoadMore(true);
            }
        } else {
            mAdapter.setList(results);
            mRefreshLayout.finishRefresh(true);
        }
        if (mAdapter.getItemCount() == 0) {
            emptyView.show("暂没有退款/售后信息", "");
        }
    }

    @Override
    public void onGetOrder(OrderDetailResult result) {

    }

    @Override
    public void onOrderPayResult(Map<String, Object> result) {

    }

    @Override
    public void onGetHouseKeeper(HouseKeeperResult result) {

    }

    @Subscribe
    public void onOrderEvent(OrderEvent event) {
        int eventId = event.getEventId();
        if (event.getEventCode() == OrderEvent.DELETE) {
            mAdapter.removeById(eventId);
        } else {
            loadData(page = 1);
        }
    }
}
